<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>微博评论</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
session_start();
include_once( 'config.php' );
include_once( 'saetv2.ex.class.php' );
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
mysql_select_db(DB_NAME,$con);
mysql_query("SET NAMES utf8");

$name='最烂IT论坛';
$content = 'test一下';
$appkeydomain = '1.zidong1750.duapp.com';
$cate='chongwu';

$nameArr = array('东经109度23分琬', '诚心感恩感', '宇宙无敌龅牙葵福');
$c=count($nameArr)-1;
$i=rand(0,$c);
$name = $nameArr[$i];
$contentArr = array('您好，喜欢宠物的朋友欢迎关注@萌乐宠官网 喵~。', '喵~喜欢宠物的朋友们欢迎关注@萌乐宠官网 嘻嘻', '您好，喜欢宠物吗？欢迎关注@萌乐宠官网 哦，各种宠物，各种卖萌', '喵一个，@萌乐宠官网 各种宠物萌图，欢迎关注。', '您如果喜欢宠物欢迎关注 @萌乐宠官网 各种好玩的宠物图片，非常萌哦~~~');
$c=count($contentArr)-1;
$i=rand(0,$c);
$content = $contentArr[$i];
//echo $content;

$sql="select * from cai_uid where name='".$name."' and domain='".$appkeydomain."' and sns='weibo'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$token = $row['token'];
//echo $token;

$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $token);
$userid=suiji($cate);
//echo $userid;
$weibo = $c->show_user_by_id($userid);
while(isset($weibo['statuses_count']) && $weibo['statuses_count'] == 0){
    $userid=suiji($cate);
    $weibo = $c->show_user_by_id($userid);
}
dump($weibo);
$w_id = $weibo['status']['id'];  //3439185933847463
//echo $w_id;

$result=$c->send_comment( $w_id ,$content, 1);
dump($result);
if(isset($result['error']) && $result['error_code'] > 0){
    echo $name.'<br/>发布失败<br/>';
    $text = "错误：".$result['error_code']."+".$result['error']."<br/>来自网址：".$appkeydomain."<br/>发送账号：".$name;
    writelog($text,1);
}else{
    echo $name.'发布成功<br/>';
    $text="微博用户：http://weibo.com/".$result['status']['user']['id']."<br/>来自网址：".$appkeydomain."<br/>发送账号：".$name;
    writelog($text,0);
}
function writelog($str,$success)
{
    include_once( 'config.php' );
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    $sql="INSERT INTO `cai_log` (`id`, `success`, `content` , `data`,`from`,`sns`) VALUES (NULL,'".$success."','".$str."','".time()."','huifu','weibo');";
    $result=mysql_query($sql);
    if($result){
        echo '记录数据成功<br/>';
    }else{
        echo '记录数据失败<br/>';
    }
}

function suiji($cate){
    include_once( 'config.php' );
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    $sql="select * from cai_fensi where done='0' and cate='".$cate."' and sns='weibo' ORDER BY rand() LIMIT 1"; 
    $result=mysql_query($sql);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $id=$row['id'];
    $user_id=$row['user_id'];
    $sql2="UPDATE `cai_fensi` SET `done` ='1' WHERE  id='".$id."' and sns='weibo'";
    $result2=mysql_query($sql2);
    if($result2){
        return $user_id;
    }else{
        return '0';
    }  
}
?>
</body>
</html>